package Thread2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author wangzhipeng
 * @Date 2020-08-21 10:20 上午
 * @Description
 */
public class TestInteger {
    public static void main(String[] args) throws InterruptedException {

        Plus p = new Plus(0);

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0;i < 10000;i++){
                    p.plus();
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0;i < 10000;i++){
                    p.plus();
                }
            }
        });

        t1.start();
        t2.start();

        Thread.sleep(1000);
        System.out.println(p.getI());

    }
}

class Plus{
    private Integer i;

   // Object lock = new Object();

    Lock lock = new ReentrantLock();

    public Plus(Integer i) {
        this.i = i;
    }

    public Integer getI() {
        return i;
    }

    public void setI(Integer i) {
        this.i = i;
    }

    public void plus(){
//        synchronized (lock){
            lock.lock();
            i++;
            lock.unlock();
      //  }
    }

}
